package com.fancy.learncenter.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.activity.base.BaseActivity;
import com.fancy.learncenter.adapter.base.CommonRecycleViewAdapter;
import com.fancy.learncenter.adapter.base.CustomViewHold;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.YgtDetailDesDataBean;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.view.PromptPopupWindow;
import com.fancy.learncenter.view.RatingBarView;
import com.superservice.lya.R;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class YgtDetailDecActivity extends BaseActivity {

    @Bind(R.id.recycleView)
    RecyclerView recycleView;
    @Bind(R.id.title_name)
    TextView titleName;
    @Bind(R.id.custom_ratingbar)
    RatingBarView customRatingbar;
    @Bind(R.id.star_num)
    TextView starNum;
    @Bind(R.id.time)
    TextView time;
    @Bind(R.id.money_tv)
    TextView moneyTv;
    @Bind(R.id.description)
    TextView description;
    @Bind(R.id.price_recycleView)
    RecyclerView priceRecycleView;
    @Bind(R.id.project_description)
    TextView projectDescription;
    @Bind(R.id.peo_num)
    TextView peoNum;
    @Bind(R.id.toolbar)
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ygt_detail_dec, getResources().getColor(R.color.toolbar_color));
        ButterKnife.bind(this);
        initToolbar("项目详情");
        goodsId = getIntent().getStringExtra("goodsId");
        classifyId = getIntent().getStringExtra("classifyId");
        getData();
    }

    YgtDetailDesDataBean ygtDetailDesDataBean;
    String goodsId = "";
    String classifyId = "";

    private void getData() {
        HashMap maps = new HashMap();
        maps.put("goodsId", goodsId);
        maps.put("classifyId", classifyId);

        HttpMehtod.getInstance().ygtGoodsDetail(maps, new IdeaObserver<BaseDataBean<YgtDetailDesDataBean>>() {

            @Override
            public void onSuccess(BaseDataBean<YgtDetailDesDataBean> listBaseDataBean) {
                ygtDetailDesDataBean = listBaseDataBean.getData();
                customRatingbar.setClickable(false);
                customRatingbar.setStar(Integer.parseInt(listBaseDataBean.getData().getStar()), true);

                starNum.setText(listBaseDataBean.getData().getStar() + "星");

                time.setText(listBaseDataBean.getData().getPeriod() + "月");

                titleName.setText(listBaseDataBean.getData().getName());

                moneyTv.setText(listBaseDataBean.getData().getSubsidy() + "元");

                description.setText("项目特点： " + listBaseDataBean.getData().getFeature());


                projectDescription.setText(listBaseDataBean.getData().getDescription_notag());

                peoNum.setText(listBaseDataBean.getData().getPeople_num() + "人选择");

                if (listBaseDataBean.getData().getOptions() != null) {

                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(YgtDetailDecActivity.this);
                    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                    priceRecycleView.setLayoutManager(linearLayoutManager);

                    priceRecycleView.setAdapter(new CommonRecycleViewAdapter<YgtDetailDesDataBean.OptionsBean>(YgtDetailDecActivity.this, R.layout.activity_ygt_des_item_text, (ArrayList<YgtDetailDesDataBean.OptionsBean>) listBaseDataBean.getData().getOptions()) {
                        @Override
                        public void bindView(CustomViewHold customViewHold, YgtDetailDesDataBean.OptionsBean item, int position) {

                            TextView title = customViewHold.getView(R.id.title);
                            title.setText(item.getGoods_option() + ":");

                            TextView num = customViewHold.getView(R.id.num);
                            num.setText("¥" + item.getMoney());
                        }
                    });
                }
                if (listBaseDataBean.getData().getCaseX() != null) {

                    GridLayoutManager linearLayoutManager = new GridLayoutManager(YgtDetailDecActivity.this, 3);
                    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                    recycleView.setLayoutManager(linearLayoutManager);

                    detailPicData = new ArrayList<>();

                    for (int i = 0; i < listBaseDataBean.getData().getCaseX().size(); i++) {
                        detailPicData.add(listBaseDataBean.getData().getCaseX().get(i).getCase_img());
                    }

                    recycleView.setAdapter(new CommonRecycleViewAdapter<YgtDetailDesDataBean.CaseBean>(YgtDetailDecActivity.this, R.layout.wd__pic_item, (ArrayList<YgtDetailDesDataBean.CaseBean>) listBaseDataBean.getData().getCaseX()) {
                        @Override
                        public void bindView(CustomViewHold customViewHold, final YgtDetailDesDataBean.CaseBean item, final int position) {

                            SimpleDraweeView pic = customViewHold.getView(R.id.pic);
                            pic.setImageURI(item.getCase_img());
                            pic.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Intent intent = new Intent(YgtDetailDecActivity.this, GalleryPicDetailActivity.class);
                                    intent.putExtra(GalleryPicDetailActivity.PIC_KEY, detailPicData);
                                    intent.putExtra(GalleryPicDetailActivity.PIC_POSITION_KEY, position);
                                    intent.putExtra(GalleryPicDetailActivity.PIC_SAVA_KEY, false);
                                    startActivity(intent);

                                }

                            });


                        }
                    });
                }
            }

            @Override
            public void onFail(int errorCode) {

            }

        });
    }

    ArrayList<String> detailPicData;

    private void ygtDelGoods() {
        HashMap maps = new HashMap();
        maps.put("goodsId", goodsId);
        maps.put("classifyId", classifyId);

        HttpMehtod.getInstance().ygtDelGoods(maps, new IdeaObserver<BaseDataBean>() {

            @Override
            public void onSuccess(BaseDataBean listBaseDataBean) {
                ToastUtil.show("项目删除成功");
                finish();
            }

            @Override
            public void onFail(int errorCode) {

            }

        });
    }

    PromptPopupWindow promptPopupWindow;

    @OnClick({R.id.delete, R.id.edit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.delete:

                if (promptPopupWindow == null) {
                    promptPopupWindow = new PromptPopupWindow(this, toolbar);
                    promptPopupWindow.setCallBack(new PromptPopupWindow.CallBack() {
                        @Override
                        public void confirm() {
                            ygtDelGoods();
//                            delForum();
                        }

                        @Override
                        public void cancle() {

                        }
                    });
                }

                promptPopupWindow.showPopupWindow();


                break;
            case R.id.edit:
                Intent intent = new Intent(this, YGTAddActivity.class);
                intent.putExtra("ygtDetailDesDataBean", ygtDetailDesDataBean);
                intent.putExtra("classifyId", classifyId);
                startActivity(intent);
                finish();
                break;
        }
    }
}
